********************************************************************************
************  This program generates the main results in the paper ************* 
********************************************************************************

clear
set more off

global root = "...\Replication_Package"

********** Table 1 **********

use "${root}\Data\corp_trade_data", clear

* Sample period
keep if year >= 1988 & year <= 2018

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

* Negative earnings
gen loss = 0
replace loss = . if E == .
replace loss = 1 if E < 0

* Book-to-market
replace BE = . if BE < 0
gen MB = lagged_mktcap/(BE*1000000)

* Winsorize
winsor2 irisk turnover dispersion MB I OP short_interest, by(year) cut(1 99) replace

* Trim Amihud ratio (Amihud, 2002)
replace amihud = . if prc <= 5
winsor2 amihud, cut(1 99) trim replace

* Size
gen size = log(lagged_mktcap)

* Summary

* Unconditional
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud, stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 1, stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 5, stat(N mean sd) c(s)

* Buy
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if (indbuy == 1 | indrep == 1), stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 1 & (indbuy == 1 | indrep == 1), stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 5 & (indbuy == 1 | indrep == 1), stat(N mean sd) c(s)

* Sell
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if (indsal == 1 | indseo == 1), stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 1 & (indsal == 1 | indseo == 1), stat(N mean sd) c(s)
tabstat irisk turnover dispersion size MB ret_6_2 short_interest loss amihud if q_composite == 5 & (indsal == 1 | indseo == 1), stat(N mean sd) c(s)

* Correlation
replace freq_8k_6 = 0 if freq_8k_6 == .
gen log_freq_8k_6 = log(1 + freq_8k_6)
gen analyst = log(1 + numest)
pwcorr irisk turnover dispersion size MB ret_6_2 short_interest loss amihud ior analyst log_freq_8k_6, sig


********** Figure 1 **********

* Panel (a)
quietly do "${root}\Functions\fig123.do" indbuy indsal 1988 2018

* Panel (b)
quietly do "${root}\Functions\fig123.do" indsal indbuy 1988 2018

* Panel (c)
quietly do "${root}\Functions\fig123.do" indrep indseo 1988 2018

* Panel (d)
quietly do "${root}\Functions\fig123.do" indseo indrep 1988 2018


********** Figure 2 **********

* Panel (a)
quietly do "${root}\Functions\fig123.do" indbuy indsal 1988 2001

* Panel (b)
quietly do "${root}\Functions\fig123.do" indsal indbuy 1988 2001

* Panel (c)
quietly do "${root}\Functions\fig123.do" indrep indseo 1988 2001

* Panel (d)
quietly do "${root}\Functions\fig123.do" indseo indrep 1988 2001


********** Figure 3 **********

* Panel (a)
quietly do "${root}\Functions\fig123.do" indbuy indsal 2002 2018

* Panel (b)
quietly do "${root}\Functions\fig123.do" indsal indbuy 2002 2018

* Panel (c)
quietly do "${root}\Functions\fig123.do" indrep indseo 2002 2018

* Panel (d)
quietly do "${root}\Functions\fig123.do" indseo indrep 2002 2018


********** Table 3 **********

* Panel A: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab3.do" indbuy indsal 1988 2018

* Panel A: Repurchases Minus SEOs
quietly do "${root}\Functions\tab3.do" indrep indseo 1988 2018

* Panel B: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab3.do" indbuy indsal 1988 2001

* Panel B: Repurchases Minus SEOs
quietly do "${root}\Functions\tab3.do" indrep indseo 1988 2001

* Panel C: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab3.do" indbuy indsal 2002 2018

* Panel C: Repurchases Minus SEOs
quietly do "${root}\Functions\tab3.do" indrep indseo 2002 2018


********** Table 4 **********

* Panel A: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab45.do" indbuy indsal 1988 2018

* Panel A: Repurchases Minus SEOs
quietly do "${root}\Functions\tab45.do" indrep indseo 1988 2018

* Panel B: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab45.do" indbuy indsal 1988 2001

* Panel B: Repurchases Minus SEOs
quietly do "${root}\Functions\tab45.do" indrep indseo 1988 2001

* Panel C: Insider Purchases Minus Insider Sales
quietly do "${root}\Functions\tab45.do" indbuy indsal 2002 2018

* Panel C: Repurchases Minus SEOs
quietly do "${root}\Functions\tab45.do" indrep indseo 2002 2018


********** Table 5 **********

* Panel A: Full
quietly do "${root}\Functions\tab45.do" indbuy_opp indsal_opp 1988 2018

* Panel A: Pre-2002
quietly do "${root}\Functions\tab45.do" indbuy_opp indsal_opp 1988 2001

* Panel A: Post-2002
quietly do "${root}\Functions\tab45.do" indbuy_opp indsal_opp 2002 2018

* Panel B: Full
quietly do "${root}\Functions\Functions\tab45.do" indbuy_rout indsal_rout 1988 2018

* Panel B: Pre-2002
quietly do "${root}\Functions\tab45.do" indbuy_rout indsal_rout 1988 2001

* Panel B: Post-2002
quietly do "${root}\Functions\tab45.do" indbuy_rout indsal_rout 2002 2018


********** Table 6 **********

use "${root}\Data\corp_trade_data", clear

* Quintiles; adjusted so that # transactions are evenly distributed across Qs
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

* Sample period
keep if year >= 1988 & year <= 2018

* Variables 
replace BE = . if BE < 0
gen mb = log(lagged_mktcap/(BE*1000000))
gen size = log(lagged_mktcap)
gen log_composite = log(1 + composite)
replace ret_6_2 = log(1 + ret_6_2)
replace ret_1_6 = log(1 + ret_1_6)
replace numest = 0 if numest == .
replace car_6 = 0 if car_6 == .
replace revision_0_6 = 0 if revision_0_6 == .
replace suescore = 0 if suescore == .
gen analyst = log(1 + numest)

* Winsorize
winsor2 suescore sue_6 revision_0_6 car_6, by(year) cut(2.5 97.5) replace


* Panel A: Analyst revisions
gen trasaction = indbuy
reghdfe revision_0_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", replace ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indsal
reghdfe revision_0_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indrep
reghdfe revision_0_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indseo
reghdfe revision_0_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)


* Panel B: SUE
replace trasaction = indbuy
reghdfe sue_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", replace ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)


replace trasaction = indsal
reghdfe sue_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indrep
reghdfe sue_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indseo
reghdfe sue_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)



* Panel C: CAR - 8K
replace trasaction = indbuy
reghdfe car_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", replace ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)


replace trasaction = indsal
reghdfe car_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indrep
reghdfe car_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indseo
reghdfe car_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != . & ret_1_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)



********** Table 7 **********


* Panel A: Without Controls
replace trasaction = indbuy
reghdfe ret_1_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", replace ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indsal
reghdfe ret_1_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indrep
reghdfe ret_1_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indseo
reghdfe ret_1_6 c.trasaction##i.q_composite mb ret_6_2 suescore size analyst if sue_6 != . & revision_0_6 != . & car_6 != ., a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)


* Panel B: With Controls
replace trasaction = indbuy
reghdfe ret_1_6 c.trasaction##i.q_composite c.sue_6#i.q_composite c.revision_0_6#i.q_composite c.car_6#i.q_composite mb ret_6_2 suescore size analyst, a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", replace ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indsal
reghdfe ret_1_6 c.trasaction##i.q_composite c.sue_6#i.q_composite c.revision_0_6#i.q_composite c.car_6#i.q_composite mb ret_6_2 suescore size analyst, a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indrep
reghdfe ret_1_6 c.trasaction##i.q_composite c.sue_6#i.q_composite c.revision_0_6#i.q_composite c.car_6#i.q_composite mb ret_6_2 suescore size analyst, a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)

replace trasaction = indseo
reghdfe ret_1_6 c.trasaction##i.q_composite c.sue_6#i.q_composite c.revision_0_6#i.q_composite c.car_6#i.q_composite mb ret_6_2 suescore size analyst, a(date siccd) cluster(siccd)
outreg2 using "${root}\Output\reg.xls", append ctitle(Future Return) stats(coef tstat) bdec(4) tdec(2)


********** Table 8 **********


use "${root}\Data\corp_trade_data", clear


* Sample period
keep if year >= 1988 & year <= 2018

* Panel A
sum low if indbuy == 1
sum low if indsal == 1
sum low if indrep == 1
sum low if indseo == 1

sum high if indbuy == 1
sum high if indsal == 1
sum high if indrep == 1
sum high if indseo == 1

sum low // Unconditional frequency of 52-week lows
sum high // Unconditional frequency of 52-week highs

* Panel B
ttest low, by(indbuy) 
ttest low, by(indsal) 
ttest low, by(indrep) 
ttest low, by(indseo) 

ttest high, by(indbuy) 
ttest high, by(indsal) 
ttest high, by(indrep) 
ttest high, by(indseo) 


********** Figure 4 **********


* Panel (a): Insider Purchases

use "${root}\Data\corp_trade_data", clear

* Sample period
keep if year >= 1988 & year <= 2018

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

forvalues q = 1/5{

	sum low if q_composite == `q' & indbuy == 1
	local low_`q' = r(mean)
	sum high if q_composite == `q' & indbuy == 1
	local high_`q' = r(mean)
	
}

clear
set obs 2 
gen prcscore = _n - 1

forvalues q = 1/5{
	gen perc_`q' = .
}

forvalues q = 1/5{
	replace perc_`q' = `low_`q'' in 1
	replace perc_`q' = `high_`q'' in 2
} 

label define prcscore1 0 "52-Week Low" 1 "52-Week High"
label values prcscore prcscore1

graph bar perc_1-perc_5, over(prcscore) ytitle("Proportion") title("Insider Purchases") leg(off)



* Panel (b): Insider Sales

use "${root}\Data\corp_trade_data", clear

* Sample period
keep if year >= 1988 & year <= 2018

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

forvalues q = 1/5{

	sum low if q_composite == `q' & indsal == 1
	local low_`q' = r(mean)
	sum high if q_composite == `q' & indsal == 1
	local high_`q' = r(mean)
	
}

clear
set obs 2 
gen prcscore = _n - 1

forvalues q = 1/5{
	gen perc_`q' = .
}

forvalues q = 1/5{
	replace perc_`q' = `low_`q'' in 1
	replace perc_`q' = `high_`q'' in 2
} 

label define prcscore1 0 "52-Week Low" 1 "52-Week High"
label values prcscore prcscore1

graph bar perc_1-perc_5, over(prcscore) ytitle("Proportion") title("Insider Sales") leg(off)


* Panel (c): Repurchases

use "${root}\Data\corp_trade_data", clear

* Sample period
keep if year >= 1988 & year <= 2018

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

forvalues q = 1/5{

	sum low if q_composite == `q' & indrep == 1
	local low_`q' = r(mean)
	sum high if q_composite == `q' & indrep == 1
	local high_`q' = r(mean)
	
}

clear
set obs 2 
gen prcscore = _n - 1

forvalues q = 1/5{
	gen perc_`q' = .
}

forvalues q = 1/5{
	replace perc_`q' = `low_`q'' in 1
	replace perc_`q' = `high_`q'' in 2
} 

label define prcscore1 0 "52-Week Low" 1 "52-Week High"
label values prcscore prcscore1

graph bar perc_1-perc_5, over(prcscore) ytitle("Proportion") title("Repurchases") leg(off)


* Panel (d): SEOs

use "${root}\Data\corp_trade_data", clear

* Sample period
keep if year >= 1988 & year <= 2018

* Quintiles
astile q_size = lagged_mktcap, by(date) nq(5)
drop if q_size == .
astile q_composite = composite, by(date q_size) nq(5)
drop if q_composite == .

forvalues q = 1/5{

	sum low if q_composite == `q' & indseo == 1
	local low_`q' = r(mean)
	sum high if q_composite == `q' & indseo == 1
	local high_`q' = r(mean)
	
}

clear
set obs 2 
gen prcscore = _n - 1

forvalues q = 1/5{
	gen perc_`q' = .
}

forvalues q = 1/5{
	replace perc_`q' = `low_`q'' in 1
	replace perc_`q' = `high_`q'' in 2
} 

label define prcscore1 0 "52-Week Low" 1 "52-Week High"
label values prcscore prcscore1

graph bar perc_1-perc_5, over(prcscore) ytitle("Proportion") title("SEOs") leg(off)




